package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TOrderGoodsEntity;
import com.gmail.blackdog1987.onedollar.entity.TOrderInfoEntity;
import com.gmail.blackdog1987.onedollar.entity.TOrderOnedollarEntity;
import com.gmail.blackdog1987.onedollar.entity.TUserEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Repository("orderOneDollarDao")
public class OrderOneDollarDao extends EntityHibernateDao<TOrderOnedollarEntity> {
    @Autowired
    private BaseJdbcDao baseJdbcDao;

    public List<TOrderOnedollarEntity> queryNewest(int onedollarid, int count) {
        return JdbcUtil.queryForList(baseJdbcDao, TOrderOnedollarEntity.class, "SELECT * FROM t_order_onedollar WHERE onedollar_id = ? and pay_status=0 and user_id is null ORDER BY random_code DESC limit ?", new Object[]{onedollarid, count});
    }

    public TOrderOnedollarEntity queryWinnerOrder(int code, int onedollarid) {

        List<TOrderOnedollarEntity> list = JdbcUtil.queryForList(baseJdbcDao, TOrderOnedollarEntity.class, "select * from t_order_onedollar where ticket_code >=? and onedollar_id = ? limit 1", new Object[]{code, onedollarid});

        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public TOrderOnedollarEntity queryRandomWinner(int onedollarid) {

        List<TOrderOnedollarEntity> list = JdbcUtil.queryForList(baseJdbcDao, TOrderOnedollarEntity.class, "select * from t_order_onedollar where onedollar_id = ? order by random_code desc limit 1", new Object[]{onedollarid});

        return list == null || list.size() == 0 ? null : list.get(0);
    }

//    public List<TOrderOnedollarEntity> queryByIds(int userid, int id, int count){
//
//        return JdbcUtil.queryForList(baseJdbcDao, TOrderOnedollarEntity.class, "SELECT * FROM t_order_onedollar WHERE onedollar_id=? AND user_id=? AND pay_status=0 order by random_code desc limit ?", new Object[]{id, userid, count});
//    }

    public void getBackOrder(int userid, int id){
        int count = baseJdbcDao.getJdbcTemplate().update("update t_order_onedollar set user_id = null WHERE onedollar_id=? AND user_id=? AND pay_status=0", new Object[]{id, userid});
        baseJdbcDao.getJdbcTemplate().update("update t_goods_onedollar set fill = (fill - "+count+") WHERE id=? ", new Object[]{id});
    }

    public int queryAvailableCount(int userid, int id){
        return baseJdbcDao.getJdbcTemplate().queryForInt("SELECT count(*) FROM t_order_onedollar WHERE onedollar_id=? AND user_id=? AND pay_status=0" , new Object[]{userid, id});
    }

    public void updatePayByIds(int userid, int id, int count, int pay_type){
        baseJdbcDao.getJdbcTemplate().update("update t_order_onedollar set  pay_status = 1, pay_type = "+pay_type+" WHERE onedollar_id=? AND user_id=? AND pay_status=0 order by random_code desc limit ?", new Object[]{id, userid, count});
        baseJdbcDao.getJdbcTemplate().update("update t_goods_onedollar set  lasttime = now() WHERE fill>=need and id = ?", new Object[]{id});
    }

    public void updateBuyByIds(int userid, int id, int count){
        baseJdbcDao.getJdbcTemplate().update("update t_order_onedollar set user_id =" + userid + ", createtime=now() WHERE onedollar_id=? AND pay_status=0 AND user_id is null order by random_code desc limit ?", new Object[]{id, count});
    }

    public boolean isSoldOut(String orderid){
       return baseJdbcDao.getJdbcTemplate().queryForInt(" select count(1) from t_order_onedollar where onedollar_id = ? and pay_status = 0" , new Object[]{orderid})==0;
    }

    public List<TOrderOnedollarEntity> queryNotPayBefore(Date date){

        return JdbcUtil.queryForList(baseJdbcDao, TOrderOnedollarEntity.class, "SELECT * FROM t_order_onedollar where user_id is not null and pay_status = 0 and createtime < ?", new Object[]{date});
    }
}
